package app.itgungnir.compose.wan.business.my_collection

import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Add
import androidx.compose.runtime.*
import androidx.compose.ui.Modifier
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.navigation.NavController
import androidx.navigation.compose.rememberNavController
import androidx.paging.compose.collectAsLazyPagingItems
import androidx.paging.compose.items
import app.itgungnir.compose.wan.widgets.WanDialog
import app.itgungnir.compose.wan.widgets.WanFab
import app.itgungnir.compose.wan.widgets.WanPagingList
import app.itgungnir.compose.wan.widgets.WanScaffold
import com.google.accompanist.insets.navigationBarsPadding

@Composable
fun MyCollectionScreen(navController: NavController = rememberNavController()) {
    val viewModel = viewModel<MyCollectionViewModel>()
    // UI
    val editDialogState = remember { mutableStateOf(false) }
    val collectionState = remember { mutableStateOf<MyCollection?>(null) }
    WanScaffold(
        modifier = Modifier.navigationBarsPadding(),
        navController = navController,
        title = "我喜欢的文章",
        loadingState = viewModel.loadingState.collectAsState(),
        loadingJob = viewModel.loadingJob,
        errorState = viewModel.errorState.collectAsState(initial = null),
        floatingActionButton = {
            WanFab(icon = Icons.Filled.Add) {
                viewModel.dismissDialogState.value = false
                collectionState.value = null
                editDialogState.value = true
            }
        }
    ) {
        val pagingItems = viewModel.myCollectionFlow.collectAsLazyPagingItems()
        WanPagingList(pagingItems = pagingItems) {
            items(pagingItems) { collection ->
                MyCollectionItem(
                    navController = navController,
                    item = collection,
                    editCallback = {
                        viewModel.dismissDialogState.value = false
                        collectionState.value = it
                        editDialogState.value = true
                    },
                    deleteCallback = {
                        viewModel.deleteCollection(it)
                    }
                )
            }
        }
    }
    CollectionEditDialog(state = editDialogState, collectionState = collectionState, viewModel = viewModel)
}

@Composable
private fun CollectionEditDialog(
    state: State<Boolean>,
    collectionState: State<MyCollection?>,
    viewModel: MyCollectionViewModel = viewModel()
) {
    WanDialog(
        state = state,
        loadingJob = viewModel.dialogLoadingJob,
        dismissState = viewModel.dismissDialogState.collectAsState(),
        errorState = viewModel.errorState.collectAsState(initial = null)
    ) {
        val originCollection = collectionState.value
        CollectionEditDialogUI(
            origin = originCollection,
            confirmCallback = { title, link, author ->
                when (originCollection == null) {
                    true -> viewModel.addCollection(title, link, author)
                    else -> viewModel.updateCollection(
                        originCollection.copy(title = title, link = link, author = author)
                    )
                }
            },
            loadingState = viewModel.dialogLoadingState.collectAsState(),
            errorState = viewModel.dialogErrorState.collectAsState(initial = null)
        )
    }
}
